<?php

namespace App\Http\Services;

use App\Exceptions\ServiceException;
use App\Exceptions\ServiceExceptionConstant;
use App\Http\Repositories\Redis\CacheDataRedis;
use App\Http\Traits\GlobalConstant;
use Illuminate\Support\Facades\Log;

class UploadService extends BaseService
{

    /**
     * 接口直接传文件
     * @return array
     * @throws ServiceException
     */
    public function upload($driver = 'local')
    {
        $array    = request()->file('file');
        $fileType = request('file_type') ?? 'image_type';
        if (empty($array)) {
            throw new ServiceException(ServiceExceptionConstant::getErrorData('请选择文件'));
        }
        $fileName = 'files' . '/' . date('Ymd');
        $type_str = CacheDataRedis::getRedisInstance()->systemConfig('upload.' . $fileType);
        $type_arr = explode(GlobalConstant::DELIMITER_CHARACTER, $type_str);
        try {
            $path = [];
            foreach ($array as $item) {
                $extension = $item->getClientOriginalExtension();
                if (!in_array($extension, $type_arr) || !in_array(strtolower($extension), $type_arr)) {
                    throw new ServiceException(ServiceExceptionConstant::getErrorData('请上传' . $type_str . '格式文件'));
                }
                $path[] = config('filesystems.disks.' . $driver . '.url') . $item->store($fileName);
            }
            return $path;
        } catch (\Exception $exception) {
            throw new ServiceException(ServiceExceptionConstant::getErrorData($exception->getMessage()));
        }
    }

    /**
     * 提交数据中上传文件
     * @param $files
     * @param string $driver
     * @param string $fileType
     * @return array|string
     * @throws ServiceException
     */
    public function dataUpload($files, string $driver = 'local', string $fileType = 'image_type')
    {
        if (empty($files)) {
            throw new ServiceException(ServiceExceptionConstant::getErrorData('请选择文件'));
        }
        $fileName = 'files' . '/' . date('Ymd');
        $type_str = CacheDataRedis::getRedisInstance()->systemConfig('upload.' . $fileType);
        $type_arr = explode(GlobalConstant::DELIMITER_CHARACTER, $type_str);
        try {
            if (is_array($files)) {
                $path = [];
                foreach ($files as $item) {
                    $extension = $item->getClientOriginalExtension();
                    if (!in_array($extension, $type_arr)) {
                        throw new ServiceException(ServiceExceptionConstant::getErrorData('请上传' . $type_str . '格式文件'));
                    }
                    $path[] = config('filesystems.disks.' . $driver . '.url') . $item->store($fileName);
                }
                return $path;
            }
            $extension = $files->getClientOriginalExtension();
            if (!in_array($extension, $type_arr)) {
                throw new ServiceException(ServiceExceptionConstant::getErrorData('请上传' . $type_str . '格式文件'));
            }
            return config('filesystems.disks.' . $driver . '.url') . $files->store($fileName);
        } catch (\Exception $exception) {
            throw new ServiceException(ServiceExceptionConstant::getErrorData($exception->getMessage()));
        }
    }
}
